﻿module Runner {
    export class Enemy extends Phaser.Sprite implements Runner.OffenseDefense {
        facing: number = 1;
        speed: number;
        aggroRadius: number;
        originalAI: EnemyAIType;
        AIType: EnemyAIType;

        lastX: number = 0;

        hitPoints: number = 20;
        maxHitPoints: number = 20;
        _lastHitPoints: number = 0;
        atkPower: number = 10;
        dodgeChance: number = 15;
        healthBar: Phaser.Graphics;

        constructor(game: Phaser.Game, x: number, y: number, typeOf: string, speed?: number, AIType?: EnemyAIType, aggroRadius: number = 0, usesPhysics:boolean = true) {
            super(game, x, y, typeOf);
            this.anchor.setTo(0.5, 0);
            
            SpriteBuilder.buildAnimation(this);
            if (usesPhysics) {
                this.physicsEnabled = true;
                SpriteBuilder.setSpritePhysics(this);
            }

            if (speed === undefined || speed == null) {
                this.speed = 60;
            } else {
                this.speed = speed;
            }

                this.aggroRadius = aggroRadius;

            if (AIType === undefined || AIType == null) {
                this.AIType = EnemyAIType.Patrol;
            } else {
                this.AIType = AIType;
            }
            this.originalAI = this.AIType;
        }

        update() {
            if (this.physicsEnabled) {
                if (this.aggroRadius > 0) {
                    this.AIType = (this.aggro()) ? EnemyAIType.Seek : this.originalAI;
                }
                if (this.body.x > this.lastX) {
                    this.animations.play('walk_right');
                } else if (this.body.x < this.lastX) {
                    this.animations.play('walk_left');
                } else {
                    this.animations.frame = 27;
                }


                switch (this.AIType) {
                    case EnemyAIType.Patrol:
                        aiWander(this);
                        break;
                    case EnemyAIType.Seek:
                        aiSeek(this, Runner.getHero(this.game));
                        break;
                    case EnemyAIType.Wander:
                        ////
                        break;
                    case EnemyAIType.Sentry:
                    default:
                    //
                }
                this.lastX = this.body.x;
            }
        }

        enemyHit() {
            this.facing = (this.facing == 0) ? 1 : 0;
        }

        aggro(): boolean {
            var distance: number = Physics.distanceBetween(this, Runner.getHero(this.game));
            if (distance < this.aggroRadius) {
                return true;
            }
            return false;
        }

    }

    export function getSprite<T>(game, bodyToFind):T{//keyToFind = 'Hero'): T {
        var hero: T;
        game.world.children.forEach(function (element) {
            if (element.body == bodyToFind) {
                hero = element;
            }
        });

        return hero;
    }
}